import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './pages/Home'
import Category from './pages/Category'
import Shopcart from './pages/Shopcart'
import User from './pages/User'
import Address from './pages/user/Address'
import AddressEdit from './pages/user/AddressEdit'
import Login from './pages/user/Login'
import Register from './pages/user/Register'
import GoodsList from './pages/goods/GoodsList'
import GoodsInfo from './pages/goods/GoodsInfo'
import OrderCreate from './pages/order/OrderCreate'
import OrderList from './pages/order/OrderList'
import OrderShow from './pages/order/OrderShow'
import NewsList from './pages/news/NewsList'
import NewsInfo from './pages/news/NewsInfo'
import PhotoList from './pages/photo/PhotoList'
import PhotoInfo from './pages/photo/PhotoInfo'

Vue.use(VueRouter)

const routes = [
        {
            path: '/', 
            redirect: '/home', 
        }, {
            path: '/home', 
            component: Home, 
            name: 'home', 
            meta: {title:'首页'}
        }, {
            path: '/category', 
            component: Category, 
            name: 'category', 
            meta: {title:'分类'}
        }, {
            path: '/shopcart', 
            component: Shopcart, 
            name: 'shopcart', 
            meta: {title:'购物车'}
        }, {
            path: '/user', 
            component: User, 
            name: 'user', 
            meta: {title:'我的'}
        }, {
            path: '/user/address', 
            component: Address, 
            name: 'address', 
            meta: {title:'收货地址'}
        }, {
            path: '/user/address/add', 
            component: AddressEdit, 
            name: 'address_add', 
            meta: {title:'添加地址'}
        }, {
            path: '/user/address/edit/:id', 
            props: true,
            component: AddressEdit, 
            name: 'address_edit', 
            meta: {title:'编辑地址'}
        }, {
            path: '/user/address/select',
            props: true,
            component: Address, 
            name: 'address_select', 
            meta: {title:'选择地址'}
        }, {
            path: '/user/login',
            component: Login,
            name: 'login', 
            meta: {title:'登录'}
        }, {
            path: '/user/register', 
            component: Register, 
            name: 'register', 
            meta: {title:'注册'}
        }, {
            path: '/goodslist/:id', 
            component: GoodsList, 
            name: 'goodslist', 
            meta: {title:'商品列表'}
        }, {
            path: '/goodsinfo/:id', 
            component: GoodsInfo, 
            name: 'goodsinfo', 
            meta: {title:'商品详情'}
        }, {
            path: '/order/create', 
            props: true,
            component: OrderCreate, 
            name: 'order_create', 
            meta: {title:'下订单'}
        }, {
            path: '/order/list', 
            component: OrderList, 
            name: 'order_list', 
            meta: {title:'订单列表'}
        }, {
            path: '/order/show/:id',
            component: OrderShow, 
            name: 'order_show', 
            meta: {title:'查看订单'}
        }, {
            path: '/news/list', 
            component: NewsList, 
            name: 'news_list', 
            meta: {title:'新闻列表'}
        }, {
            path: '/news/info/:id',
            component: NewsInfo, 
            name: 'news_info', 
            meta: {title:'新闻资讯详情'}
        }, {
            path: '/photo/list', 
            component: PhotoList, 
            name: 'photo_list', 
            meta: {title:'图片列表'}
        }, {
            path: '/photo/info/:id', 
            props: true,
            component: PhotoInfo, 
            name: 'photo_info', 
            meta: {title:'图片资讯详情'}
        }
    ]

const router = new VueRouter({
    routes,
    // 给当前的路由添加一个类名
    linkActiveClass: 'mui-active'
})

// 根据路由地址设置页面title
router.beforeEach((to, from, next) => {
    if(to.meta.title) {
        document.title = to.meta.title
    }
    // 放行函数
    next();
})


export default router